import urllib2
from gi.repository import GObject, Gtk, Gedit
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers

UI_XML = """<ui>
<menubar name="MenuBar">
<menu name="ToolsMenu" action="Tools">
<placeholder name="ToolsOps_3">
<menu name="Submenu" action="SubmenuAction">
<menuitem name="ISAction1" action="ISAction1"/>
<menuitem name="ISAction" action="ISAction"/>
<menuitem name="ISAction2" action="ISAction2"/>
</menu>
</placeholder>
</menu>
</menubar>
</ui>"""


class interviewstreet(GObject.Object, Gedit.WindowActivatable):
    __gtype_name__ = "interviewstreet"
    window = GObject.property(type=Gedit.Window)
    email=" "
    invite=" "
    path=" "    
   
    def __init__(self):
        GObject.Object.__init__(self)	
    
    def _add_ui(self):
        manager = self.window.get_ui_manager()
        self._actions = Gtk.ActionGroup("Example04Actions")
	self._actions.add_actions([ ('SubmenuAction', None, "Interview Street",None, "This is a submenu", None),
        ('ISAction', Gtk.STOCK_REFRESH, "Sync", "<control>u", "Sync code online", self.on_IS_action_activate),
        ('ISAction1', Gtk.STOCK_CONNECT, "Connect", None, "Connect to Invite", self.on_IS1_action_activate),
        ('ISAction2', Gtk.STOCK_GOTO_BOTTOM, "Download", None, "Download", self.on_IS2_action_activate), ])
        manager.insert_action_group(self._actions)
        self._ui_merge_id = manager.add_ui_from_string(UI_XML)
        manager.ensure_update()
        
    def do_activate(self):
        self._add_ui()

    def do_deactivate(self):
        self._remove_ui()

    def do_update_state(self):
   	pass

    def on_IS_action_activate(self, action, data=None):
	global email
	global invite
	if(email==" " and invite==" "):
		print "You are not connected. Please try to connect and then Sync."
	else:
		register_openers()
		datagen, headers = multipart_encode({'file': open(self.window.get_active_document().get_uri_for_display(), "rb")})
		query = "http://localhost/Interviewstreet/Website/source/login1.php?email="+email+"&invite="+invite+"&func=2"
		request = urllib2.Request(query, datagen, headers)
		ret=urllib2.urlopen(request).read()
		if(ret!="Done"):
			print "There was an error in sync. You might want to try again"		
		else:
			print "Sync successful"
	
    def on_IS1_action_activate(self, action, data=None):
	global email
	global invite
	print "*************************************************************************\n"
	print "Enter email id: "
	email=raw_input()
	print "\nEnter invite id: "
	invite=raw_input()
	query = "http://localhost/Interviewstreet/Website/source/login1.php?email="+email+"&invite="+invite+"&func=1"
	ret=urllib2.urlopen(query).read()
	if(ret=="Success"):
		print "Connected. Ready to Sync!"
	else:
		print "Invalid email or invite ID"	
		email=" "
		invite=" "

    def on_IS2_action_activate(self, action, data=None):
	global email
	global invite
	if(email==" " and invite==" "):
		print "You are not connected. Please try to connect and then Sync."
	else:
		query = "http://localhost/Interviewstreet/Website/source/login1.php?email="+email+"&invite="+invite+"&func=3"
		ret=urllib2.urlopen(query).read()
		view = self.window.get_active_view()
	        if view:
        	    view.get_buffer().insert_at_cursor(ret)

    def _remove_ui(self):
        manager = self.window.get_ui_manager()
        manager.remove_ui(self._ui_merge_id)
        manager.remove_action_group(self._actions)
        manager.ensure_update()
